package cn.yuanqiao.web.controller.archive.supervision;

import cn.yuanqiao.archive.supervision.domain.AnnualVo;
import cn.yuanqiao.archive.supervision.domain.YqAttachments;
import cn.yuanqiao.archive.supervision.domain.YqAttachmentsNotices;
import cn.yuanqiao.archive.supervision.service.AnnualInspectionNoticeService;
import cn.yuanqiao.archive.uploadfile.util.FileUtils;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * 年检通知创建
 *
 * @author : 庸人一个
 * @create : 2024/3/13 15:22
 */
@RestController
@RequestMapping("/archive/annu")
public class AnnualInspectionNoticeController extends BaseController {

    @Autowired
    private AnnualInspectionNoticeService annualInspectionNoticeService;

    //添加
    @PostMapping("/save")
    public AjaxResult save(@RequestBody AnnualVo vo) {
//        if(StringUtils.isNotEmpty(vo.getNoticeContent())) {
//            try {
//                String decode = URLDecoder.decode(vo.getNoticeContent(), "UTF-8");
//                vo.setNoticeContent(decode);
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//                throw new RuntimeException("html处理失败");
//            }
//        }
        return toAjax(annualInspectionNoticeService.save(vo));
    }

    //上传附件
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile file, String type, Long id, @RequestParam(value="mj",required=false)String mj) {
        return annualInspectionNoticeService.uploadFile(file, type,id,mj);
    }

    //下载附件
    @GetMapping("/downloadFile")
    public void downloadFile(HttpServletResponse response, Long id) {
        YqAttachments attachments = annualInspectionNoticeService.findByFileId(id);
        File file = new File(attachments.getFilePath());
       //把此文件发送到页面
       try {
            response.setContentType("application/octet-stream");
           response.setCharacterEncoding("utf-8");
           response.setHeader("Content-Disposition", "attachment;filename=" + attachments.getFileName());
           response.getOutputStream().write(FileUtils.readFileToByteArray(file));
           response.getOutputStream().flush();
           response.getOutputStream().close();
           System.gc();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    //删除附件
    @DeleteMapping("/deleteFile")
    public AjaxResult deleteFile(@RequestBody YqAttachmentsNotices attachmentsNotices) {
        return toAjax(annualInspectionNoticeService.deleteFile(attachmentsNotices));
    }


    //删除
    @DeleteMapping("/delete")
    public AjaxResult delete(Long id) {
        return toAjax(annualInspectionNoticeService.deleteById(id));
    }

    //修改
    @PutMapping("/update")
    public AjaxResult update(@RequestBody AnnualVo vo) {
//        if(StringUtils.isNotEmpty(vo.getNoticeContent())) {
//            try {
//                String decode = URLDecoder.decode(vo.getNoticeContent(), "UTF-8");
//                vo.setNoticeContent(decode);
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//                throw new RuntimeException("html处理失败");
//            }
//        }
        return toAjax(annualInspectionNoticeService.update(vo));
    }

    //分页查询
    @PostMapping("/findPage")
    public TableDataInfo findPage(@RequestBody AnnualVo vo) {
        List<AnnualVo> annualVoList = annualInspectionNoticeService.findPage(vo);
        return getDataTable(annualVoList);
    }

    //id
    @GetMapping("/findById")
    public AjaxResult findById(Long id) {
        return success(annualInspectionNoticeService.findById(id));
    }

    //下发年检通知
    @PostMapping("/send")
    public AjaxResult send(@RequestBody JSONObject object) {
        annualInspectionNoticeService.send(object);
        return success();
    }

    //下发年检通知单位查询
    @PostMapping("/findUnit")
    public AjaxResult findUnit(@RequestBody Map<String, Object> map) {
        return success(annualInspectionNoticeService.findUnit(map));
    }

}
